(define (rand x)
  (cond ((eq? x 'generate) (begin
			     (define ret
			       (cons-stream random-init
					    (stream-map rand-update ret)))
			     ret))
	 ((eq? x 'reset) (lambda (x)
			  (let ()
			    (define ret
			      (cons-stream x
					   (stream-map rand-update ret)))
			    ret)))))
(define (pairs x y)
  
(define (estimate-integral p generator)
  (define mont
    (stream-map (lambda (x)
		  (if (p x)
		      1
		      0))
		generator))
  (define -mont
    (cons-stream (stream-car mont)
		 (add-streams (stream-cdr mont) -mont)))
  (define ones (cons-stream 1 ones))
  (define integrates 1 (add-streams ones integrates))
  
  (stream-map / -mont integrates))

(define pi
  (let ((r 3))
    (define (square l) (* l l))
    (define (p alist)
      (let ((a (car alist))
	    (b (cadr alist)))
	(< (+ (square (- a r)) (square (- b r))) (square r))))
    (define (pair x y)
      (stream-map (lambda (x y) (list x y)) x y))
;    (define possible-stream
;      (stream-filter (lambda (c) (and (> c 0) (< c (* 2 r))))
;		     (rand 'generator)))
    (define generator
      (pair (stream-filter (lambda (x) (and (> x 0) (< x (* 2 r)))) (rand 'generate))
	    (stream-filter (lambda (x) (and (> x 0) (< x (* 2 r)))) (rand 'generate))))
    (estimate-integral p generator)))
    
